using System;
using System.Xml;
using System.Collections;
using System.ComponentModel;
using Omniture.Studio.EventArgs;

namespace Omniture.Studio.Objects
{
  public delegate void MessageDelegate(object sender, MessageEventArgs e);

  public class MessageCollection : CollectionBase
  {
    #region Member Variables
    
    #endregion

    #region Constructor

    #endregion

    #region Events

    public event MessageDelegate AfterMessageAdd;
    public event MessageDelegate AfterMessageRemove;

    protected void OnAfterMessageAdd(object sender, MessageEventArgs e)
    {
      if (this.AfterMessageAdd != null)
        this.AfterMessageAdd(sender, e);
    }

    protected void OnAfterMessageRemove(object sender, MessageEventArgs e)
    {
      if (this.AfterMessageRemove != null)
        this.AfterMessageRemove(sender, e);
    }
    
    #endregion

    #region Property Implementations

    #endregion

    #region IList Members

		public Message this[int index]
		{
			get { return (Message)this.InnerList[index]; }
			set { this.InnerList[index] = value; }
		}

    public new void RemoveAt(int index)
    {
      Message element = this[index];
      this.InnerList.RemoveAt(index);
			this.OnAfterMessageRemove(this, new Omniture.Studio.EventArgs.MessageEventArgs(element));
    }

    public void Insert(int index, Message value)
    {
      this.InnerList.Insert(index, value);
    }

    public void Remove(Message value)
    {
      this.InnerList.Remove(value);
			this.OnAfterMessageRemove(this, new Omniture.Studio.EventArgs.MessageEventArgs(value));
    }

    public bool Contains(Message value)
    {
      return this.InnerList.Contains(value);
    }

    public int IndexOf(Message value)
    {
      return this.InnerList.IndexOf(value);
    }

    internal int Add(Message value)
    {
      int retval = this.InnerList.Add(value);
			this.OnAfterMessageAdd(this, new Omniture.Studio.EventArgs.MessageEventArgs(value));
      return retval;
    }

    public Message Add(MessageTypeConstants messageType, string text)
    {
      Message newItem = new Message(messageType, text);
      this.Add(newItem);
      return newItem;
    }

    public void AddRange(ICollection list)
    {
      this.InnerList.AddRange(list);
      foreach(Message message in list)
				this.OnAfterMessageAdd(this, new Omniture.Studio.EventArgs.MessageEventArgs(message));
    }

    #endregion

  }
}
